package com.example.docking.service.impl;

import com.example.docking.dao.jindie.GenericDataItem;
import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.EmpData;
import com.example.docking.dao.jindie.response.SaveData;
import com.example.docking.service.EmpService;
import com.example.docking.util.Constants;
import com.example.docking.util.EmpNumber;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class EmpServiceImpl implements EmpService {
    private static final Logger logger = LoggerFactory.getLogger(EmpServiceImpl.class);

    @Override
    public String getEmpId(DataServiceImpl service, String empName) throws Exception {
        Gson gson = new Gson();
        ResponseData<SaveData> responseData = new ResponseData<>();
        // 设置分页参数
        int page = 1; // 初始页码
        int pageSize = 10; // 每页记录数
        HashMap<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        map.put("page_size", String.valueOf(pageSize));
        String emp_id = "";

        ApiResult EmpDataResult = null;
        // 调用分页查询方法获取职员数据
        EmpDataResult = service.SelectDataList(Constants.EMP, map, "");
        Type EmpDataType = new TypeToken<ResponseData<GenericDataItem<EmpData>>>() {
        }.getType();
        ResponseData<GenericDataItem<EmpData>> responseEmpData = gson.fromJson(EmpDataResult.getBody(), EmpDataType);
        GenericDataItem<EmpData> genericDataItemEmp = responseEmpData.getData();
        // 解析分页信息
        String count = genericDataItemEmp.getCount();
        int countNum = Integer.valueOf(count);
        int Pagesum = pageSize;
        List<EmpData> empData = genericDataItemEmp.getRows();

        List<EmpData> allEmpData = new ArrayList<>(empData);
        // 如果不是最后一页，继续获取下一页数据
        while (Pagesum < countNum) {
            page++;
            map.put("page", String.valueOf(page));
            EmpDataResult = service.SelectDataList(Constants.EMP, map, "");
            responseEmpData = gson.fromJson(EmpDataResult.getBody(), EmpDataType);
            genericDataItemEmp = responseEmpData.getData();
            empData = genericDataItemEmp.getRows();
            allEmpData.addAll(empData);
            Pagesum = (pageSize * page);
        }

        boolean empFound = false;
        for (EmpData empDataItem : allEmpData) {
            if (empDataItem.getName().equals(empName)) {
                emp_id = empDataItem.getId();
                empFound = true;
                break;
            }
        }

        if (!empFound) {
            // 新增职员逻辑，这里可进一步完善必填参数的设置
            EmpData empDataInsert = new EmpData();
            empDataInsert.setName(empName);
            // 根据业务需求完善以下参数设置
            EmpNumber numberGen = new EmpNumber();
            empDataInsert.setNumber(String.valueOf(numberGen.generateOrderNumber()));// 需根据实际业务确定正确值
            EmpDataResult = service.InsertDataOne(Constants.EMP, map, empDataInsert);
            Type SaveDataType = new TypeToken<ResponseData<SaveData>>() {
            }.getType();
            logger.info("EmpServiceImpl：插入职员" + empName);
            responseData = gson.fromJson(EmpDataResult.getBody(), SaveDataType);
            if (responseData.getErrcode() == 0) { // 假设0表示成功
                SaveData saveData = responseData.getData();
                emp_id = saveData.getIds().get(0);
            }
            logger.info("EmpServiceImpl：插入职员:Code: " + responseData.getErrcode());
            logger.info("EmpServiceImpl：插入职员:Message: " + responseData.getDescription());
            logger.info("EmpServiceImpl：插入职员:data: " + responseData.getData());
        }
        return emp_id;
    }


}